package xyz.bali16.application.config;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import xyz.bali16.application.config.prop.AuthProp;

/**
 * @author bali2 on 15/4/2023.
 * @version 1.0
 */

@Configuration
@AllArgsConstructor
public class TokenConfig {

    private final AuthProp authProp;
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }
    //
    @Bean
    protected JwtAccessTokenConverter accessTokenConverter() {
        String jwtSigningKey = authProp.getJwtSigningKey();
        if (StrUtil.isEmpty(jwtSigningKey)) {
            jwtSigningKey = AuthProp.DEFAULT_JWT_SIGNING_KEY;
        }
        JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
        accessTokenConverter.setSigningKey(jwtSigningKey);
        return accessTokenConverter;
    }

}
